<template>
  <div class="user-header">
    <div class="avatar">
      <div class="list-img-box" v-if="props.userInfo.userId === userStore.userMessage.userId">
        <div class="upload-btn" @click="uploadPicture">
          <img :src="userStore.userMessage.userBust" />
        </div>
      </div>
      <div v-else>
        <img :src="props.userInfo.userBust" />
      </div>
      <!-- 剪裁组件弹窗 -->
      <el-dialog title="头像上传" v-model="dialogVisible" width="950px" center>
        <AvatarPictureCropper @finish="handleClose"></AvatarPictureCropper>
      </el-dialog>
    </div>
    <div class="signature">
      <div v-if="editing" style="flex-grow: 1">
        <el-input
          v-model="userStore.userMessage.userVita"
          style="margin: 5px; max-width: 700px"
          placeholder="请输入个性签名"
          size="large"
          type="text"
          maxlength="50"
          show-word-limit
        />
      </div>
      <div v-else style="flex-grow: 1; margin-left: 20px; font-size: 14px; color: #6a6e77">
        {{ props.userInfo?.userVita || '这个人很懒，什么都没留下' }}
      </div>
      <el-button
        v-if="props.userInfo.userId === userStore.userMessage.userId"
        :type="editing ? 'success' : 'primary'"
        :icon="editing ? Check : Edit"
        circle
        @click="editing ? saveSignature(userStore.userMessage.userVita) : toggleEdit()"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { Check, Edit } from '@element-plus/icons-vue'
import useUserStore from '@/store/user/user'
import AvatarPictureCropper from './avatar-picture-cropper.vue'

const props = defineProps(['userInfo'])
const emit = defineEmits(['updateUserVita'])
//从userStore获取信息
const userStore = useUserStore()
//裁剪对话框是否显示
const dialogVisible = ref(false)

//是否有图片
const hasImg = ref(false)

// 控制是否处于编辑状态
const editing = ref(false)

//图片设置
function uploadPicture() {
  dialogVisible.value = true
}
const handleClose = () => {
  dialogVisible.value = false
  hasImg.value = true
}

// 切换编辑状态
const toggleEdit = () => {
  editing.value = !editing.value
}

// 保存签名到用户状态
const saveSignature = (signature: any) => {
  userStore.userMessage.signature = signature
  editing.value = false
  const ruleForm = reactive({
    userId: userStore.userMessage.userId,
    userBust: userStore.userMessage.userBust,
    userVita: userStore.userMessage.userVita,
    nickname: userStore.userMessage.nickname,
    username: userStore.userMessage.username,
    password: userStore.userMessage.password,
    userEmail: userStore.userMessage.userEmail,
    phoneNumber: userStore.userMessage.phoneNumber,
    skill: userStore.userMessage.skill,
    score: userStore.userMessage.score,
    age: userStore.userMessage.age,
    sex: userStore.userMessage.sex
  })
  console.log('ruleForm', ruleForm)
  userStore.updateUserInformation(ruleForm)
  emit('updateUserVita', userStore.userMessage.userVita)
}
</script>

<style scoped>
.avatar {
  display: flex;
  flex-wrap: wrap;
  margin: 0 auto 0 0;
  position: relative;
  .list-img-box {
    position: relative;
    cursor: pointer;
    .upload-btn {
      border-radius: 50%;
    }
  }
  img {
    height: 150px;
    width: 150px;
    border-radius: 50%;
    border: #dcdfe6 1px solid;
  }
  .list-img-box::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    border-radius: 50%;
    pointer-events: none;
    background-color: rgba(0, 0, 0, 0.5);
    opacity: 0;
    transition: opacity 0.3s ease;
  }

  .list-img-box::before {
    content: '\00a0\00a0\00a0\00a0'
      url('data:image/svg+xml;charset=UTF-8,<svg t="1715420248151" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4264" width="25" height="25"><path d="M955.076923 610.461538h-59.076923c-15.753846 0-29.538462 15.753846-29.538462 29.538462v196.923077c0 15.753846-13.784615 29.538462-29.538461 29.538461h-649.846154c-15.753846 0-29.538462-13.784615-29.538461-29.538461v-196.923077c0-13.784615-13.784615-29.538462-29.538462-29.538462h-59.076923c-15.753846 0-29.538462 15.753846-29.538462 29.538462V905.846154c0 43.323077 35.446154 78.769231 78.769231 78.769231h787.692308c43.323077 0 78.769231-35.446154 78.769231-78.769231V640c0-13.784615-13.784615-29.538462-29.538462-29.538462zM531.692308 47.261538c-11.815385-11.815385-29.538462-11.815385-41.353846 0L224.492308 313.107692c-11.815385 11.815385-11.815385 29.538462 0 41.353846l41.353846 41.353847c11.815385 11.815385 29.538462 11.815385 41.353846 0l110.276923-110.276923c11.815385-11.815385 35.446154-3.938462 35.446154 13.784615v417.476923c0 15.753846 11.815385 29.538462 27.569231 29.538462h59.076923c15.753846 0 31.507692-15.753846 31.507692-29.538462V301.292308c0-17.723077 19.692308-25.6 33.476923-13.784616l110.276923 110.276923c11.815385 11.815385 29.538462 11.815385 41.353846 0l41.353847-41.353846c11.815385-11.815385 11.815385-29.538462 0-41.353846L531.692308 47.261538z" fill="%23FFFFFF" p-id="4265"></path></svg>')
      '\A头像上传';
    white-space: pre;
    position: absolute;
    top: 50%;
    left: 50%;
    color: white;
    transform: translate(-50%, -50%);
    pointer-events: none;
    z-index: 1;
    opacity: 0;
    transition: opacity 0.3s ease;
  }

  .list-img-box:hover::after,
  .list-img-box:hover::before {
    opacity: 1;
  }
}
.upload-btn {
  display: -webkit-box;
  display: -ms-flexbox;
  background: url('@/assets/img/User.png') no-repeat 50%;
  display: flex;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  border: 1px solid #cccccc;
  border-radius: 5px;
  overflow: hidden;
  box-shadow: 0 0 1px #cccccc;
}
.upload-btn:hover {
  border: 1px solid #69b7ed;
}
.signature {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 150px;
  width: 100%;
  padding: 0 10px;
  white-space: nowrap; /* 防止内容换行 */
}
</style>
